package com.thb.pay.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.thb.pay.api.sys.SysRoleServiceApi;
import com.thb.pay.api.sys.vo.req.SysRolePageReq;
import com.thb.pay.api.sys.vo.req.SysRoleReq;
import com.thb.pay.api.sys.vo.resp.SysRoleVo;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.system.entity.SysRole;
import com.thb.pay.system.service.SysRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 系统-角色表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class SysRoleController implements SysRoleServiceApi {

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public PageResult<SysRoleVo> findPage(@RequestBody SysRolePageReq pageReq) {
        return sysRoleService.findPage(pageReq);
    }

    @Override
    public void addRole(@RequestBody SysRoleReq sysRole) {
        SysRole targetRole = new SysRole();
        BeanUtils.copyProperties(sysRole, targetRole);
        sysRoleService.insert(targetRole);
    }

    @Override
    public void updateRole(@RequestBody SysRoleReq sysRole) {
        SysRole targetRole = new SysRole();
        BeanUtils.copyProperties(sysRole, targetRole);
        sysRoleService.updateById(targetRole);
    }

    @Override
    public SysRoleVo getByRoleId(@RequestParam("roleId") Long roleId) {

        SysRole sysRole = sysRoleService.selectById(roleId);
        if (sysRole != null) {
            SysRoleVo sysRoleVo = new SysRoleVo();
            BeanUtils.copyProperties(sysRole, sysRoleVo);
            return sysRoleVo;
        }
        return null;
    }

    @Override
    public List<SysRoleVo> findAllRoles() {
        List<SysRoleVo> roleVoList = Lists.newArrayList();
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SysRole::getCreateTime);
        List<SysRole> roleList = sysRoleService.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(roleList)) {
            roleList.forEach(sysRole -> {
                SysRoleVo sysRoleVo = new SysRoleVo();
                BeanUtils.copyProperties(sysRole, sysRoleVo);
                roleVoList.add(sysRoleVo);
            });
        }
        return roleVoList;
    }

    @Override
    public void deleteBatchRoleIds(@RequestBody List<Long> roleIds) {
        sysRoleService.deleteBatchIds(roleIds);
    }
}
